//
//  GearsView.m
//  BalanceCar
//
//  Created by 郑信鸿 on 17/6/27.
//  Copyright © 2017年 郑信鸿. All rights reserved.
//

#import "GearsView.h"
#import "GearsItemView.h"

@interface GearsView ()<UIGestureRecognizerDelegate>

@property(nonatomic, strong)GearsItemView *itemView;

@property(nonatomic, strong)UITapGestureRecognizer *tapGesture;

@property(nonatomic, assign)GearsItemNum gearsNum;

@end

@implementation GearsView


- (instancetype)init{
    
    if (self = [super init]) {
        //40x140
        UIImageView *backImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dashboard_gear_bg"]];
        [self addSubview:backImage];
        [backImage mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.edges.mas_offset(0);
            
        }];
        self.itemView = [[GearsItemView alloc]init];
        [self addSubview:self.itemView];
        [self.itemView mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.mas_offset(0);
            make.centerY.mas_offset(0);
            make.width.height.mas_offset(40);
        }];
        [self establishGestureRecognizer];
    }
    
    return self;
}


- (void)establishGestureRecognizer{
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:self.tapGesture];
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
    
    [self addGestureRecognizer:panGesture];
}

- (void)swipeAction:(UIPanGestureRecognizer *)pan{

    if (pan.state == UIGestureRecognizerStateEnded) {
        
        CGPoint offset = [pan translationInView:self];
        CGFloat x = offset.x;
        if (fabs(x) < 10) {
            return;
        }
        if (x > 0) {
            if (self.gears == 3) {
                return;
            }else{
                self.gears ++;
            }
        }else{
            
            if (self.gears == 1) {
                return;
            }else{
                self.gears --;
            }
            
        }
        if ([_delegate respondsToSelector:@selector(gearsView:gears:)]) {
            
            [_delegate gearsView:self gears:self.gearsNum];
        }
    }
    
}

- (void)tapAction:(UITapGestureRecognizer *)tap{
    CGPoint point = [tap locationInView:self];
    CGFloat x = point.x;
    
    if (x > 40 && x < 50) {
        return;
    }
    
    if (x > 90 && x < 100) {
        return;
    }
    
    CGAffineTransform transfrom = CGAffineTransformIdentity;
    if (x < 40) {
        //1档
        self.gearsNum = GearsItemOne;
        transfrom = CGAffineTransformIdentity;
    }else if (x > 50 && x < 90){
        //2档
        self.gearsNum = GearsItemTwo;
        transfrom = CGAffineTransformMakeTranslation(50, 0);
    }else if(x > 100 && x < 140){
        //3档
        self.gearsNum = GearsItemThree;
        transfrom = CGAffineTransformMakeTranslation(100, 0);
    }
    
    
    if (_itemView.gearsNum == self.gearsNum) {
        
        return;
    }
    _itemView.gearsNum = self.gearsNum;
    _itemView.transform = transfrom;
    if ([_delegate respondsToSelector:@selector(gearsView:gears:)]) {
        
        [_delegate gearsView:self gears:self.gearsNum];
    }
    
}

- (void)setGears:(NSInteger)gears{
    _gears = gears;
    CGAffineTransform transfrom = CGAffineTransformIdentity;
    switch (gears) {
        case 1:
        {
             self.gearsNum = GearsItemOne;
            transfrom = CGAffineTransformIdentity;
        }
            break;
        case 2:{
            self.gearsNum = GearsItemTwo;
             transfrom = CGAffineTransformMakeTranslation(50, 0);
        }
            break;
        case 3:{
             self.gearsNum = GearsItemThree;
            transfrom = CGAffineTransformMakeTranslation(100, 0);
        }
            break;
        default:
            break;
    }
    _itemView.transform = transfrom;
    _itemView.gearsNum = self.gearsNum;
}

@end
